﻿
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using CMSWCSFSolution.CMS.Interface.Services;
using CMS.Model;
using CMS.ViewModel;
namespace CMSWCSFSolution.CMS.Views
{
    public class CourseManagePresenter : Presenter<ICourseManageView>
    {
        private ICMSService _iCMSService;
        private int _rowCount;

        /// <summary>
        /// Creates Presenter
        /// </summary>
        /// <param name="iCMSService"></param>
        public CourseManagePresenter([CreateNew] ICMSService iCMSService)
        {
            _iCMSService = iCMSService;
        }

        /// <summary>
        /// Select Course By Condition
        /// </summary>
        public List<CoursePE> GetCoursesListByCondition()
        {
            List<CoursePE> returnCourseList = new List<CoursePE>();
            List<Course> courseList = _iCMSService.GetCoursesByCondition(View.CourseName, View.CourseStatus, View.PageIndex, View.PageSize, out _rowCount);
            foreach (var course in courseList)
            {
                CoursePE coursePE = new CoursePE();
                coursePE.CourseId = course.CourseId;
                coursePE.CourseName = course.CourseName;
                coursePE.TeacherName = course.Teacher.TeacherName;
                coursePE.LevelName = course.Level.LevelName;
                if (course.CourseStatus == true)
                {
                    coursePE.CourseStatus = "Open";
                }
                else
                {
                    coursePE.CourseStatus = "Closed";
                }
                returnCourseList.Add(coursePE);
            }
            View.RowCount = _rowCount;
            return returnCourseList;
        }

        /// <summary>
        /// Changes Course Status
        /// </summary>
        public void ChangeCourseStatus()
        {
            string[] courseIdList = View.CourseIdList;
            if (courseIdList != null)
            {
                for (var i = 0; i < courseIdList.Length; i++)
                {
                    Course course = new Course();
                    course.CourseId = Convert.ToInt32(courseIdList[i]);
                    _iCMSService.ChangeCourseStatus(course);
                }
            }

        }

        /// <summary>
        /// Deletes Course By Id
        /// </summary>
        public bool DeleteCourse()
        {
            int courseId = View.CourseId;
            var course = _iCMSService.GetCourseById(courseId);
            if (course.Count != 0)
            {
                _iCMSService.DeleteCourseById(courseId);
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}




